layui.use(['form', 'table','layuimini','element'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layuimini = layui.layuimini;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    var BoArticle = {
        id: 'BoArticleTable'
    };

    var column = [
        [
            {type: 'numbers'},
            {field: 'title', title: '标题', align: 'center'},
            {field: 'remark', title: '简介', align: 'center',
                templet: function (object) {
                    if(object.remark == '' || object.remark == null){
                        return '-';
                    }else{
                        return object.remark;
                    }
                }
            },
            {field: '', title: '内容', align: 'center',
                templet: function (object) {
                    return '<span style="color: #FF5722;">内容不显示</span>';
                }
            },
            {field: 'img', title: '文章封面图', align: 'center',
                templet: function (object) {
                    var img = '<img src="/article_image/' + object.img +
                        '" lay-event="previewImg"' + ' style="cursor: pointer;max-width:100px; max-height:100px;"';
                    return img;
                }
            },
            {field: 'readNum', title: '阅读数量', align: 'center', width: 100},
            {field: 'praiseNum', title: '赞数量', align: 'center', width: 100},
            {field: 'boTagList', title: '标签', align: 'center',
                templet: function (object) {
                    var boTag = object.boTagList;
                    if(boTag.length > 0){
                        var html = "";
                        for(var i = 0; i < boTag.length; i++){
                            html += '<span style="margin:0 5px;color:' + boTag[i].color + '">' + boTag[i].name + '</span>';
                        }
                        return html;
                    }else{
                        return '-';
                    }
                }
            },
            {field: 'isTop', title: '推荐', align: 'center', width: 80,
                templet: function (object) {
                    if(object.isTop == 0){
                        return '<span class="layui-badge layui-bg-green">正常</span>';
                    }else{
                        return '<span class="layui-badge">推荐</span>';
                    }
                }
            },
            {field: 'type', title: '发布类型', align: 'center', width: 100,
                templet: function (object) {
                    if(object.type == 0){
                        return '<span class="layui-badge layui-bg-green">博主发布</span>';
                    }else{
                        return '<span class="layui-badge">游客发布</span>';
                    }
                }
            },
            {field: 'userName', title: '发布人', align: 'center',
                templet: function (object) {
                    if(object.userName == '' || object.userName == null){
                        return '-';
                    }else{
                        return object.userName;
                    }
                }
            },
            {field: 'isPass', title: '审核状态', align: 'center', width: 100,
                templet: function (object) {
                    if(object.isPass == 0){
                        return '<span class="layui-badge layui-bg-orange">审核中</span>';
                    }else if(object.isPass == 1){
                        return '<span class="layui-badge layui-bg-green">通过</span>';
                    }else {
                        return '<span class="layui-badge layui-bg-gray">不通过</span>';
                    }
                }
            },
            {field: 'updateDate', title: '更新时间', minWidth: 120, align: 'center'},
            {field: 'createDate', title: '发布时间', minWidth: 120, align: 'center'},
            {templet: '#stateBtn', width: 170, align: 'center', title: '操作'}
        ]
    ];

    table.render({
        elem: '#' + BoArticle.id,
        url: '/web/boArticle/recycleList',
        toolbar: '#toolbarBtn',
        defaultToolbar: ['filter', 'exports', 'print'],
        cols: column,
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true
    });

    /**
     * 点击彻底删除按钮
     * @param id
     */
    BoArticle.thoroughlyDelete = function(id){
        layer.open({
            title: '彻底删除文章'
            ,content: '是否确定彻底删除该文章？'
            ,icon: 3
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                layer.close(index);
                var indexLoad = layer.load(2);
                var url = '/web/boArticle/thoroughlyDelete';
                $.post(url, {id: id}, function(data){
                    layer.close(indexLoad);
                    if(data.state == "200"){
                        layer.msg(data.message, {icon: 1});
                        table.reload(BoArticle.id);
                    }else{
                        layer.msg(data.message, {icon: 2});
                    }
                })
            }
        });

        return false;
    };

    /**
     * 点击恢复按钮
     * @param id
     */
    BoArticle.restore = function(id){
        layer.open({
            title: '恢复文章'
            ,content: '是否确定恢复该文章？'
            ,icon: 3
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                layer.close(index);
                var indexLoad = layer.load(2);
                var url = '/web/boArticle/restore';
                $.post(url, {id: id}, function(data){
                    layer.close(indexLoad);
                    if(data.state == "200"){
                        layer.msg(data.message, {icon: 1});
                        table.reload(BoArticle.id);
                    }else{
                        layer.msg(data.message, {icon: 2});
                    }
                })
            }
        });

        return false;
    };

    //封面图预览
    BoArticle.previewImg = function(data) {

        var width = (window.screen.width - 400) + 'px';
        var height = (window.screen.height - 300) + 'px';

        var imgHtml = "<img src='/article_image/" + data.img + "' style='min-width:100px; min-height:100px;max-width:" + width + ";max-height:" + height + ";'/>";
        //捕获页
        layer.open({
            type: 1,
            title: false, //不显示标题
            shadeClose: true,
            skin: 'previewImg-class', //透明
            area: ['auto', 'auto'],
            content: imgHtml //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
        });
    };

    //监听表格复选框选择
    // table.on('checkbox(currentTableFilter)', function (obj) {
    //     console.log(obj)
    // });

    //监听工具条
    table.on('tool(' + BoArticle.id + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'thoroughlyDelete') {
            BoArticle.thoroughlyDelete(data.id);
        }else if(layEvent === 'restore'){
            BoArticle.restore(data.id);
        }else if(layEvent === 'previewImg'){
            BoArticle.previewImg(data);
        }
    });

});